//
//  LGPatientCareTemplateController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/9/19.
//  模版导入任务

#import "LGPatientCareTemplateController.h"
//出院病情证明书
#import "LGRecordHospitalController.h"
#import "LGRecordHospitalController2.h"

#import "LGConsultRecordDiseaseCell.h"
#import "LGCareTemplateOtherCell.h"
#import "LGCarePlanTaskHeadView.h"
#import "LGPickerViewMax.h"
#import "DatePickerView.h"
@interface LGPatientCareTemplateController ()<UITableViewDelegate,UITableViewDataSource,LGConsultRecordDiseaseDelegate,LGCareTemplateOtherDelegate>
/**   */
@property (nonatomic , strong)UIView *headView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *diseasesArray;
/**   */
@property (nonatomic , strong)NSArray *planArray;
/**   */
@property (nonatomic , copy)NSString *planName;
/**   */
@property (nonatomic , copy)NSString *planid;
/**   */
@property (nonatomic , copy)NSString *selectDate;
/**   */
@property (nonatomic , copy)NSString *height;
/**   */
@property (nonatomic , copy)NSString *weight;
/**   */
@property (nonatomic , copy)NSString *xiaohdjb;
/**   */
@property (nonatomic , copy)NSString *dabyx;
/**   */
@property (nonatomic , copy)NSString *killip;

/**   */
@property (nonatomic , copy)NSString *reportid;
/**   */
@property (nonatomic , copy)NSString *version;

@end

@implementation LGPatientCareTemplateController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationView.title = @"模版导入任务";
    [self.view addSubview:self.tableView];
    [self createBottomView];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.care_plan/importCarePlanDetail" parameters:@{@"patient_id":self.patientid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.diseasesArray = returnData[@"diseasesAry"];
            self.planid = LGNSString(returnData[@"care_plan_id"]);
            self.planArray = returnData[@"care_plan_list"];
            self.selectDate = LGNSString(returnData[@"reference_date"]);
            self.height = LGNSString(returnData[@"height"]);
            self.weight = LGNSString(returnData[@"weight"]);
            self.xiaohdjb = LGNSString(returnData[@"xiaohdjb"]);
            self.dabyx = LGNSString(returnData[@"dabyx"]);
            self.killip = LGNSString(returnData[@"killip"]);
            self.reportid = LGNSString(returnData[@"lastest_report"]);
            self.version = LGNSString(returnData[@"lastest_version"]);
            CGFloat height = (self.reportid.length>0)?viewPix(27):0.01;
            self.headView.frame = CGRectMake(0, 0, Screen_W, height);
            self.tableView.tableHeaderView = self.headView;
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)setPlanArray:(NSArray *)planArray{
    _planArray = planArray;
    for (NSDictionary *tempDic in planArray) {
        NSString *planid = LGNSString(tempDic[@"plan_id"]);
        if([planid isEqualToString:self.planid]){
            self.planName = LGNSString(tempDic[@"title"]);
        }
    }
}

//查看最新一条报告
-(void)checkReportDeatail{
    if([self.version isEqualToString:@"1"]){
        LGRecordHospitalController *controller = [[LGRecordHospitalController alloc]init];
        controller.recordId = self.reportid;
        [self.navigationController pushViewController:controller animated:YES];
    }else{
        LGRecordHospitalController2 *controller = [[LGRecordHospitalController2 alloc]init];
        controller.recordId = self.reportid;
        [self.navigationController pushViewController:controller animated:YES];
    }
}

//选择类型
-(void)selectTypeAction{
    NSMutableArray *pickArray = [NSMutableArray array];
    for (NSDictionary *tempDic in self.planArray) {
        [pickArray addObject:LGNSString(tempDic[@"title"])];
    }
    LGPickerViewMax *pickerView = [[LGPickerViewMax alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.pickArry = @[pickArray];
    pickerView.selectStr = self.planName;
    [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
    __weak typeof(self) weakSelf = self;
    pickerView.selectPickIndexResult = ^(NSArray *resultArray, NSArray *indexArray) {
        weakSelf.planName = LGNSString(resultArray[0]);
        weakSelf.planid = LGNSString(weakSelf.planArray[[indexArray[0] integerValue]][@"plan_id"]);
        [weakSelf.tableView reloadData];
    };
}

//选择时间
-(void)selectDateAction{
    DatePickerView *pickerView = [[DatePickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.currentDate = self.selectDate;
    [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
    __weak typeof(self) weakSelf = self;
    pickerView.selectDate = ^(NSString *date) {
        weakSelf.selectDate = date;
        [weakSelf.tableView reloadData];
    };
}

//取消
-(void)cancelButtonAction{
    [self.navigationController popViewControllerAnimated:YES];
}

//保存
-(void)saveDataRequest{
    if(self.planid.length == 0){
        [TooltipView showMessage:@"请选择照护计划模版" offset:0];
        return;
    }
    
    if(self.selectDate.length == 0){
        [TooltipView showMessage:@"请选择基准时间" offset:0];
        return;
    }
    
    [RequestUtil withPOST:@"/doctor/patient.care_plan/importCarePlanSave" parameters:@{@"patient_id":self.patientid,@"care_plan_id":self.planid,@"reference_date":self.selectDate,@"diseasesAry":[ObjectTool objectToJson:self.diseasesArray],@"height":self.height,@"weight":self.weight,@"xiaohdjb":self.xiaohdjb,@"dabyx":self.dabyx,@"killip":self.killip} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            [TooltipView showMessage:@"保存成功" offset:0];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:@"保存失败，请重试" offset:0];
    }];
}

//共病
-(void)diseaseItemSelect:(NSArray *)resultArray{
    self.diseasesArray = resultArray;
}

//其他
-(void)templateOtherSelect:(NSString *)content  key:(NSString *)key{
    if([key isEqualToString:@"xiaohdjb"]){
        self.xiaohdjb = content;
    }else if ([key isEqualToString:@"dabyx"]){
        self.dabyx = content;
    }else{
        self.killip = content;
    }
}

//其他-身高、体重
-(void)templateOtherInput:(NSString *)content key:(NSString *)key{
    if([key isEqualToString:@"height"]){
        self.height = content;
    }else{
        self.weight = content;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section<2){
        return 0;
    }else{
        return 1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 2){
        //共病
        LGConsultRecordDiseaseCell *diseaseCell = [tableView dequeueReusableCellWithIdentifier:@"diseaseCell" forIndexPath:indexPath];
        diseaseCell.selectionStyle = UITableViewCellSelectionStyleNone;
        diseaseCell.resultArray = self.diseasesArray;
        diseaseCell.delegate = self;
        return diseaseCell;
    }else{
        //其他
        LGCareTemplateOtherCell *otherCell = [tableView dequeueReusableCellWithIdentifier:@"otherCell" forIndexPath:indexPath];
        otherCell.selectionStyle = UITableViewCellSelectionStyleNone;
        otherCell.height = self.height;
        otherCell.weight = self.weight;
        otherCell.xiaohdjb = self.xiaohdjb;
        otherCell.dabyx = self.dabyx;
        otherCell.killip = self.killip;
        otherCell.delegate = self;
        return otherCell;
    }
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return (section<2)?viewPix(46):viewPix(56);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    CGFloat height = (section<2)?viewPix(46):viewPix(56);
    NSArray *titleArray = @[@"照护计划模板",@"基准时间",@"共病类型(可多选)",@"其他"];
    LGCarePlanTaskHeadView *headView = [[LGCarePlanTaskHeadView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, height)];
    headView.title = titleArray[section];
    if(section == 0){
        headView.showTip = YES;
        headView.content = self.planName;
        [headView.actionBtn addTarget:self action:@selector(selectTypeAction) forControlEvents:UIControlEventTouchUpInside];
    }else if (section == 1){
        headView.showTip = YES;
        headView.content = self.selectDate;
        [headView.actionBtn addTarget:self action:@selector(selectDateAction) forControlEvents:UIControlEventTouchUpInside];
    }else{
        headView.showTip = NO;
        headView.content = @"";
    }
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    CGFloat height = (section<3)?viewPix(15):0.01;
    return height;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if(section<3){
        LGCornerFooterView *footView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
        footView.backgroundColor = [UIColor clearColor];
        footView.shadowView.cornerRidus = 12;
        footView.bottomMargin = viewPix(8);
        return footView;
    }else{
        return [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    }
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight-viewPix(60)-bottomSafeBarHeight/3.0) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGConsultRecordDiseaseCell class] forCellReuseIdentifier:@"diseaseCell"];
        [_tableView registerClass:[LGCareTemplateOtherCell class] forCellReuseIdentifier:@"otherCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headView;
    }
    return _tableView;
}

-(UIView *)headView{
    if(!_headView){
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(27))];
        _headView.backgroundColor = [UIColor colorWithString:@"#FEF7EE"];
        _headView.clipsToBounds = YES;
        UIButton *button = [UIButton buttonWithTitle:@"查看最新出院病情证明书" titleFont:LGFont(15) textColor:@"#333333" imageName:@"grayTip12"];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button.frame = CGRectMake(viewPix(16), 0, Screen_W-viewPix(30), viewPix(27));
        [button setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:5];
        [button addTarget:self action:@selector(checkReportDeatail) forControlEvents:UIControlEventTouchUpInside];
        [_headView addSubview:button];
    }
    return _headView;
}


-(void)createBottomView{
    UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, kMaxY(self.tableView.frame), Screen_W, viewPix(60)+bottomSafeBarHeight/3.0)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    //编辑
    UIButton *deleteBtn = [UIButton buttonWithTitle:@"取消" titleFont:LGFontWeight(16, UIFontWeightMedium) normalColor:@"#429BFF" selectColor:@"#429BFF"];
    deleteBtn.frame = CGRectMake(viewPix(47), viewPix(8), (Screen_W-viewPix(118))/2.0, viewPix(46));
    deleteBtn.layer.borderColor = [UIColor colorWithString:@"#429BFF"].CGColor;
    deleteBtn.layer.borderWidth = 1;
    deleteBtn.cornerRidus = 10;
    [deleteBtn addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:deleteBtn];
    
    UIButton *saveBtn = [UIButton buttonWithTitle:@"保存" titleFont:LGFontWeight(16, UIFontWeightMedium) normalColor:@"#FFFFFF" selectColor:@"#FFFFFF"];
    saveBtn.frame = CGRectMake(kMaxX(deleteBtn.frame)+viewPix(24), viewPix(8), (Screen_W-viewPix(118))/2.0, viewPix(46));
    saveBtn.backgroundColor = [UIColor colorWithString:@"#429BFF"];
    saveBtn.cornerRidus = 10;
    [saveBtn addTarget:self action:@selector(saveDataRequest) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:saveBtn];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
